// pages/mails/mails1/mails1.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
            // appData: getApp().globalData,
            // currentCollectAddress: getApp().globalData.sendExpressData['currentCollectAddress'],
            // currentSenAddress: getApp().globalData.sendExpressData['currentSenAddress'],
            // sendType: getApp().globalData.sendExpressData['sendType'],
            // goodsInfo: getApp().globalData.sendExpressData['currentSenAddgoodsInforess'],

            // 控件数据
            pickerRange: [], // 上门时间选择器
            pickerRangeValue: [0, 0],
            clause: false, // 电子条款
            buttonBtn: false, // 下单按钮，默认是不能点击的
            animationData: {}, // 预估总价动画
            animationData1: {}, // 预估总价动画
            animationFlag: false // 判断动画是否打开
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            const app = getApp();
            let sendExpressData = app.globalData.sendExpressData;
            this.setPickerInitValue(true) // 初始化上门时间选项数据
            this.createAnimation() // 创建动画
      },

      // 上门时间
      setPickerInitValue(isToday) {
            let date = [{
                  key: '今天',
                  name: '今天',
            }, {
                  key: '明天',
                  name: '明天',
            }, {
                  key: '后天',
                  name: '后天',
            }];
            let time = [];
            let start = '09';
            let end = '19';
            if (isToday) {
                  time.push({
                        name: '一小时内',
                        key: '一小时内'
                  })
                  start = new Date().getHours();
                  for (let i = start; i <= Number(end); i++) {
                        start = Number(start)
                        time.push({
                              name: start + ':00-' + (start + 1) + ':00',
                              key: start + ':00-' + (start + 1) + ':00',
                        })
                        start++;
                  }
            } else {
                  for (let i = 9; i <= Number(end); i++) {
                        start = Number(start)
                        time.push({
                              name: start + ':00-' + (start + 1) + ':00',
                              key: start + ':00-' + (start + 1) + ':00',
                        })
                        start++;
                  }
            }
            let data = []
            data.push(date)
            data.push(time)
            this.setData({
                  pickerRange: data
            })
      },
      pickerFn(e) { // 取消事件
            // console.log(e)
            this.setData({
                  pickerRangeValue: this.data.pickerRangeValue
            })
      },
      pickerchange(e) { // 列滚动事件
            // console.log(e)
            var {
                  column,
                  value
            } = e.detail
            if (this.pickerchange.flag != value && column == 0) {
                  this.pickerchange.flag = value;
                  if (column == 0 && value != 0) {
                        this.setPickerInitValue(false) // 初始化上门时间选项数据
                  } else {
                        this.setPickerInitValue(true) // 初始化上门时间选项数据
                  }
            }
      },
      changeValue(e) { // 单击确认事件--期望上门时间
            // console.log(e)
            let pickerRangeValue = e.detail.value;
            this.appData['sendType']['date'] = this.data.pickerRange[0][pickerRangeValue[0]]['key']
            this.appData['sendType']['time'] = this.data.pickerRange[1][pickerRangeValue[1]]['key']
            this.setData({
                  pickerRangeValue: pickerRangeValue,
                  sendType: this.appData['sendType']
            })
            // console.log(this.appData)
      },

      /* 下单 */
      complete() {
            if (!this.data['goodsInfo']['goodsName']) {
                  wx.navigateTo({
                        url: '../goodsInfo/goodsInfo'
                  })
            }
      },


      // 跳转到新的页面
      detailAddr(e) {
            let address = e.currentTarget.dataset.addr;
            if (!address) return;
            // console.log(address)
            wx.navigateTo({
                  url: address
            })
      },
      // 跳转到地址簿页面
      choolAddressBook(e) {
            let this_ = this;
            let address = e.currentTarget.dataset.addr;
            let type = e.currentTarget.dataset.type;
            if (!address) return;
            console.log(address)
            wx.navigateTo({
                  url: address,
                  events: {
                        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                        acceptDataFromOpenedPage: function (res) {
                              // console.log(res)
                              if (res.type == 'send') {
                                    this_.appData['currentSenAddress'] = res.data
                              } else {
                                    this_.appData['currentCollectAddress'] = res.data
                              }
                              this_.initData()
                        }
                  },
                  success: function (res) {
                        // 通过eventChannel向被打开页面传送数据
                        res.eventChannel.emit('acceptDataFromOpenerPage', {
                              "type": type
                        })
                  }
            })
      },

      //  表单提交
      formSubmit2(e) {
            console.log('form发生了submit事件，携带数据为：', e.detail)
            // console.log('form发生了submit事件，携带数据为：', e)
      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            this.initData()
      },
      /* 每次打开页面都次刷新数据 */
      initData() {
            const app = getApp().globalData;
            let sendExpressData = app['sendExpressData'];
            this.appData = sendExpressData;
            // console.log('sendExpressData')
            // console.log(sendExpressData)
            this.setData({
                  currentSenAddress: sendExpressData['currentSenAddress'],
                  currentCollectAddress: sendExpressData['currentCollectAddress'],
                  sendType: sendExpressData['sendType'],
                  goodsInfo: sendExpressData['goodsInfo'],
            })
      },

      // 用于控制电子条款
      clauseFn() {
            this.setData({
                  clause: !this.data.clause,
                  buttonBtn: !!this.data['goodsInfo']['goodsName'] && !this.data.clause
            })
      },

      // 动画--控制预估总价
      createAnimation() {
            let animation = wx.createAnimation({
                  delay: 200,
                  duration: 1000,
                  timingFunction: 'ease'
            }) 
            let animation1 = wx.createAnimation({
                  delay: 200,
                  duration: 40000,
                  timingFunction: 'ease'
            })
            this.animation = animation
            this.animation1 = animation1
      },
      openAnimation() {
            let animationFlag = this.data['animationFlag']
            if (animationFlag) {
                  this.animation.height('auto').step()
                  this.animation1.height(0).step()
                  this.setData({
                        animationData: this.animation.export(),
                        animationData1: this.animation1.export(),
                        animationFlag: !animationFlag
                  })
            } else {
                  this.animation.height('100%').step()
                  this.animation1.height('auto').step()
                  this.setData({
                        animationData: this.animation.export(),
                        animationData1: this.animation1.export(),
                        animationFlag: !animationFlag
                  })
            }

      },
})